package com.qf.controller;

import com.qf.pojo.*;
import com.qf.service.DtsBrandService;
import com.qf.service.DtsCategoryService;
import com.qf.service.DtsGoodsService;
import com.qf.util.ResponseUtil;
import com.qf.vo.CatVo;
import com.qf.vo.GoodsAllinone;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/goods")
public class AdminGoodsController {

    @Autowired
    DtsGoodsService goodsService;

    @Autowired
    DtsBrandService brandService;

    @Autowired
    DtsCategoryService categoryService;

    @PostMapping("/update")
    public Object update(@RequestBody GoodsAllinone goodsAllinone){
        goodsService.update(goodsAllinone);

        return ResponseUtil.ok();
    }

    @PostMapping("/delete")
    public Object delete(@RequestBody DtsGoods goods){
        goodsService.delete(goods);

        return ResponseUtil.ok();
    }

    @GetMapping("/detail")
    public Object detail(@RequestParam("id") Integer goodsId){
        Map<String,Object> data = goodsService.detail(goodsId);

        return ResponseUtil.ok(data);
    }

    @PostMapping("/create")
    public Object create(@RequestBody GoodsAllinone goodsAllinone){
        //检验数据是否为空
        Object result = checkGoodsAllinone(goodsAllinone);

        if (result != null){
            return  result;
        }

        try {
            goodsService.insert(goodsAllinone);
        }catch (Exception e){
            e.printStackTrace();
            return  ResponseUtil.fail(-1,e.getMessage());
        }

        return ResponseUtil.ok();

    }

    private Object checkGoodsAllinone(GoodsAllinone goodsAllinone) {
        //商品名称和编号
        DtsGoods goods = goodsAllinone.getGoods();
        if (goods == null || StringUtils.isEmpty(goods.getGoodsSn()) || StringUtils.isEmpty(goods.getName())){
            return ResponseUtil.fail(-1,"商品名称和编号不能为空");
        }
        //商品属性
        DtsGoodsAttribute[] attributes = goodsAllinone.getAttributes();
        if (attributes != null && attributes.length > 0){
            for (DtsGoodsAttribute attribute : attributes) {
                if (StringUtils.isEmpty(attribute.getAttribute())){
                    return ResponseUtil.fail(-1,"商品属性不能为空");
                }
            }
        }
        //商品规格名称
        DtsGoodsSpecification[] specifications = goodsAllinone.getSpecifications();
        if (specifications != null && specifications.length > 0){
            for (DtsGoodsSpecification specification : specifications) {
                if (StringUtils.isEmpty(specification)){
                    return ResponseUtil.fail(-1,"商品规格名称不能为空");
                }
            }
        }
        //商品库存
        DtsGoodsProduct[] products = goodsAllinone.getProducts();
        if (products != null && products.length > 0){
            for (DtsGoodsProduct product : products) {
                if (StringUtils.isEmpty(product)){
                    return ResponseUtil.fail(-1,"商品库存不能为空");
                }
            }
        }
        //商品品牌
        Integer brandId = goods.getBrandId();
        if (brandId != null){
            DtsBrand brand = brandService.findById(brandId);
            if (brand == null){
                return ResponseUtil.fail(-1,"商品品牌不存在");
            }
        }
        //商品分类
        Integer categoryId = goods.getCategoryId();
        if (categoryId != null){
            DtsCategory category = categoryService.findById(categoryId);
            if (category == null){
                return ResponseUtil.fail(-1,"商品分类不存在");
            }
        }

        return null;
    }

    @GetMapping("/catAndBrand")
    public Object catAndBrand(){
        //查询分类列表
        List<CatVo> categoryList = categoryService.findCategoryList();
        //查看品牌列表
        List<Map<String,Object>> brandList = brandService.findBrandList();
        //封装
        Map<String,Object> data = new HashMap<>();
        data.put("categoryList",categoryList);
        data.put("brandList",brandList);

        return ResponseUtil.ok(data);
    }

    @GetMapping("/list")
    public Object list(@RequestParam(value = "page",required = false,defaultValue = "1")Integer page,
                       @RequestParam(value = "limit",required = false,defaultValue = "10")Integer limit,
                       @RequestParam(value = "sort",required = false,defaultValue = "add_time")String sort,
                       @RequestParam(value = "order",required = false,defaultValue = "desc")String order,
                       String goodsSn,String name){
        //获取当前登录用户
        Subject subject = SecurityUtils.getSubject();
        DtsAdmin admin = (DtsAdmin) subject.getPrincipal();
        List<Integer> brandIds = null;

        if (subject.hasRole("品牌制造商")){
            //通过品牌制造商id查找对应的品牌
            brandIds = brandService.findBrandIdsByAdminId(admin.getId());
        }

        Map<String, Object> data = goodsService.findByPage(page, limit, sort, order, goodsSn, name,brandIds);

        return ResponseUtil.ok(data);
    }
}
